package com.qlslylq.ad.sdk.config;

import android.content.Context;
import android.text.TextUtils;

import com.mbridge.msdk.MBridgeSDK;
import com.mbridge.msdk.out.MBridgeSDKFactory;
import com.mbridge.msdk.out.SDKInitStatusListener;
import com.qlslylq.ad.sdk.application.AdMergeSDK;
import com.qlslylq.ad.sdk.enums.AdPlatformEnum;
import com.qlslylq.ad.sdk.enums.ErrorEnum;
import com.qlslylq.ad.sdk.interfaces.InitCallback;
import com.qlslylq.ad.sdk.util.GsonUtils;
import com.qlslylq.ad.sdk.util.Log;

import java.util.Map;

import static com.qlslylq.ad.sdk.model.rsp.MediaWebVO.NOT_CONFIG;


/**
 * Mintegral初始化控制类<br/>
 */
public class MintegralMgrHolder {

    private static boolean initSuccess = false;

    public static void init(final Context context, final InitCallback callback) {
        doInit(context, callback);
    }

    /**
     * 初始化<br/>
     */
    private static void doInit(Context context, final InitCallback callback) {
        AdPlatformEnum platform = AdPlatformEnum.PLATFORM_MINTEGRAL;
        if (!initSuccess) {
            try {
                String appId = AdMergeSDK.getMediaWebVO().getAppIdByPlatform(platform);
                String appKey = AdMergeSDK.getMediaWebVO().getAppKeyByPlatform(platform);
                if (NOT_CONFIG.equals(appId) || NOT_CONFIG.equals(appKey)) {
                    Log.d(String.format("未配置%s账号到蒂烨平台", platform.getId()));
                    return;
                }
                if (TextUtils.isEmpty(appId) || TextUtils.isEmpty(appKey)) {
                    Log.d(String.format("%s初始化失败：" + GsonUtils.gsonString(ErrorEnum.ERR_INIT_INVALID_ARG.rsp(platform, null)), platform.getId()));
                    if (callback != null) {
                        callback.fail(platform, ErrorEnum.ERR_INIT_INVALID_ARG.getId(), ErrorEnum.ERR_INIT_INVALID_ARG.getName());
                    }
                    return;
                }

                MBridgeSDK sdk = MBridgeSDKFactory.getMBridgeSDK();
                sdk.init(buildConfig(sdk, platform), context, new SDKInitStatusListener() {
                    public void onInitSuccess() {
                        initSuccess = true;
                        Log.d(String.format("%sSDK初始化完成：" + (initSuccess ? "成功" : "失败"), platform.getId()));
                    }

                    public void onInitFail(String msg) {
                        Log.d(String.format("%sSDK初始化完成：" + (initSuccess ? "成功" : "失败") + "：" + ErrorEnum.ERR_INIT_THIRD_SDK.getId() + "：" + msg, platform.getId()));
                        if (callback != null) {
                            callback.fail(platform, ErrorEnum.ERR_INIT_THIRD_SDK.getId(), msg);
                        }
                    }
                });

            } catch (Exception e) {
                e.printStackTrace();
                Log.d(String.format("%sSDK初始化完成：" + (initSuccess ? "成功" : "失败") + "：" + e.getMessage(), platform.getId()));
                if (callback != null) {
                    callback.fail(platform, ErrorEnum.ERR_INIT_THIRD_SDK.getId(), e.getMessage());
                }
            }
        }
    }

    private static Map<String, String> buildConfig(MBridgeSDK sdk, AdPlatformEnum platform) {
        String appId = AdMergeSDK.getMediaWebVO().getAppIdByPlatform(platform);
        String appKey = AdMergeSDK.getMediaWebVO().getAppKeyByPlatform(platform);
        return sdk.getMBConfigurationMap(appId, appKey);
    }

    public static boolean isInitSuccess() {
        return initSuccess;
    }

    /**
     * 设置是否启用平台<br/>
     */
    public static void enablePlatform(boolean enable) {
        initSuccess = enable;
    }
}
